home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / msg / do.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  13.9 KB  |  475 lines

  1. /*    $Header: do.c,v 4.1 88/06/21 14:02:38 bianchi Exp $
  2.     $Source: /tmp/mgrsrc/demo/msg/RCS/do.c,v $
  3. */
  4. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/msg/RCS/do.c,v $$Revision: 4.1 $";
  5.  
  6. /*                        Copyright (c) 1987 Bellcore
  7.  *                            All Rights Reserved
  8.  *       Permission is granted to copy or use this program, EXCEPT that it
  9.  *       may not be sold for profit, the copyright notice must be reproduced
  10.  *       on copies, and credit should be given to Bellcore where it is due.
  11.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  12.  */
  13. /* do a service */
  14.  
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include "term.h"
  18. #include "do.h"
  19.  
  20. #define dprintf        if (debug) fprintf
  21. #define have_icon(icon)    (icon->w && icon->h)
  22. #define GET_OPT(i)    \
  23.     strlen(argv[i])>2 ? argv[i]+2 : argv[++i]
  24.  
  25. #define MSG        '&'        /* message leader */
  26.  
  27. #define TIMEOUT        3        /* need first response by now */
  28.  
  29. #define NORMAL        0        /* quiescent icon */
  30. #define RUNNING        1        /* icon while running command */
  31. #define ACTIVE        2        /* icon while window active */
  32.  
  33. /* remote options */
  34.  
  35. #define NONE    0            /* no remotes at all */
  36. #define LOCAL    1            /* command runs locally, ship file */
  37. #define REMOTE    2            /* command runs remotely */
  38.  
  39. int debug;
  40. int wx,wy,border;            /* initial window parameters */
  41. int fw, fh;                /* font size */
  42. struct icon *icon;            /* current icon */
  43.  
  44. main(argc,argv)
  45. int argc;
  46. char **argv;
  47.    {
  48.    register int i;
  49.  
  50.    char *index(), *ttyname(), *getenv();
  51.    int timeout(),clean();
  52.  
  53.    struct icon icons[3];        /* icons */
  54.    char line[100];            /* mgr inout buffer */
  55.    char file[100];            /* command args go here */
  56.    char message[100];            /* place to format messages */
  57.    char temp[100];            /* temp file name */
  58.    char do_host[32];            /* our host */
  59.  
  60.    char action;                /* message command */
  61.    char *client_host;            /* client host */
  62.    char *command;            /* command to run */
  63.    char *name = NULL;            /* server name (if not command) */
  64.    char *path;                /* path part of file name */
  65.  
  66.    int id;                /* id of sender */
  67.    int code;                /* return code from command */
  68.    int xpos, ypos;            /* where command window goes */
  69.    int wide ,high;            /* command window size */
  70.    int nowindow = 0;            /* no command window */
  71.    int font=0;                /* font in leiu of icon */
  72.    int wait=0;                /* wait for ack at command term. */
  73.    int remote=NONE;            /* remote requests permitted */
  74.    int icon_count=0;            /* Number of icons */
  75.    int servers = 0;            /* number of servers out there */
  76.    int bailout = 0;            /* bail out w/out running command */
  77.  
  78.    debug = (int) getenv("DEBUG");
  79.  
  80.    /* check for valid environment */
  81.  
  82.    if (argc < 3) {
  83.       fprintf(stderr,"usage: %s [options] -c<command> icon1 icon2 [icon3]\n",
  84.               *argv);
  85.       fprintf(stderr,"options:\n");
  86.       fprintf(stderr,"   -C nnn   number of columns\n");
  87.       fprintf(stderr,"   -R nnn   number of rows\n");
  88.       fprintf(stderr,"   -f n     font\n");
  89.       fprintf(stderr,"   -n name  name of command (if not command name)\n");
  90.       fprintf(stderr,"   -p       wait for CR at command termination\n");
  91.       fprintf(stderr,"   -W       don't make alternate window\n");
  92.       fprintf(stderr,"   -x nnn   starting window position\n");
  93.       fprintf(stderr,"   -y nnn   starting window position\n");
  94.       fprintf(stderr,"   -i       interactive starting icon position\n");
  95.       fprintf(stderr,"   -r       permit remote invocations\n");
  96.       fprintf(stderr,"   -N       remote invocations run over there1\n");
  97.       exit(1);
  98.       }
  99.  
  100.    ckmgrterm( *argv );
  101.    
  102.    /* setup mgr */
  103.  
  104.    m_setup(M_FLUSH);
  105.    chmod(ttyname(2),0600);    /* this is not gauranteed */
  106.    m_push(P_POSITION|P_FONT|P_EVENT|P_FLAGS);
  107.  
  108.    signal(SIGTERM,clean);
  109.    signal(SIGINT,clean);
  110.    signal(SIGHUP,clean);
  111.    signal(SIGALRM,timeout);
  112.  
  113.    m_ttyset();
  114.    m_setmode(M_NOWRAP);
  115.    m_setmode(M_ABS);
  116.    m_func(B_COPY);
  117.  
  118.    m_setevent(ACCEPT,"&%f:%m\r");
  119.    m_setevent(REDRAW,"R\r");
  120.    m_setevent(RESHAPE,"S\r");
  121.    m_setevent(MOVE,"M\r");
  122.    m_setevent(ACTIVATE,"A\r");
  123.    m_setevent(DEACTIVATE,"D\r");
  124.  
  125.    get_size(&wx,&wy,&wide,&high);
  126.    get_param(0,0,0,&border);
  127.    font = get_font(&fw,&fh);
  128.    dprintf(stderr,"%d,%d %dx%d font %d (%d,%d)\n",wx,wy,wide,high,font,fw,fh);
  129.  
  130.    xpos = wx, ypos=wy;
  131.    gethostname(do_host,sizeof(do_host));
  132.  
  133.    /* check arguments */
  134.  
  135.    for(i=1;i<argc;i++) {
  136.       if (*argv[i] == '-')
  137.          switch (argv[i][1]) {
  138.             case 'W':                /* don't make alt. window */
  139.                nowindow++;
  140.                break;
  141.             case 'r':                /* remote file, local command */
  142.                remote=LOCAL;
  143.                break;
  144.             case 'N':                /* remote commmand,local file */
  145.                remote=REMOTE;
  146.                break;
  147.             case 'p':                /* wait for ack */
  148.                wait++;
  149.                break;
  150.             case 'n':                /* specify command name */
  151.                name = GET_OPT(i);
  152.                break;
  153.             case 'c':                /* specify command */
  154.                command = GET_OPT(i);
  155.                break;
  156.             case 'f':                /* specify font */
  157.                font = atoi(GET_OPT(i));
  158.                break;
  159.             case 'C':                /* specify columns */
  160.                wide = 2*border + fw * atoi(GET_OPT(i));
  161.                break;
  162.             case 'R':                /* specify rows */
  163.                high = 2*border + fh * atoi(GET_OPT(i));
  164.                break;
  165.             case 'w':                /* specify wide */
  166.                wide = atoi(GET_OPT(i));
  167.                break;
  168.             case 'i':                /* specify icon start */
  169.                {
  170.                   int x,y;
  171.                   m_push(P_EVENT);
  172.                   m_setevent(BUTTON_1,"Z %p\r");
  173.                   fprintf(stderr,"Click button 1 to indicate icon position\n");
  174.                   while (*m_gets(line) != 'Z')
  175.                      ;
  176.                   sscanf(line,"Z %d %d",&x,&y);
  177.                   wx += x, wy +=y;
  178.                   m_pop();
  179.                   }
  180.                break;
  181.             case 'x':                /* specify x_start */
  182.                xpos = atoi(GET_OPT(i));
  183.                break;
  184.             case 'y':                /* specify y_start */
  185.                ypos = atoi(GET_OPT(i));
  186.                break;
  187.             default:
  188.                fprintf(stderr,"%s: bad flag %c ignored\n",argv[0],argv[i][1]);
  189.             }
  190.       else if (icon_count < 3) {
  191.          download_icon(&icons[icon_count],argv[i],icon_count+1,font);
  192.          icon_count++;
  193.          }
  194.       else
  195.          fprintf(stderr,"%s: invalid arg [%s] ignored\n",argv[0],argv[i]);
  196.       }
  197.  
  198.    if (RUNNING > icon_count) {
  199.       fprintf(stderr,"%s: Not enough icons specified\n",argv[0]);
  200.       exit(2);
  201.       }
  202.       
  203.    if (debug) 
  204.       getchar();
  205.  
  206.    set_icon(&icons[NORMAL]);
  207.  
  208.    if (name == NULL)
  209.       name = command;
  210.  
  211.    m_setevent(NOTIFY,name);
  212.    sprintf(message,"%c %s",S_HI,name);
  213.  
  214.    m_broadcast(message);
  215.    alarm(TIMEOUT);
  216.    m_clearmode(M_ACTIVATE);
  217.    m_flush();
  218.    dprintf(stderr,"%s: broadcast id\r\n",name);
  219.          
  220.    while(m_gets(line) != NULL) {
  221.  
  222.       /* process messages */
  223.  
  224.       if (*line == MSG) {
  225.          sscanf(line+1,"%d:%c %[^\n]", &id,&action,file);
  226.          dprintf(stderr,"%s id: %d message: %s\r\n",name,id,file);
  227.          switch (action) {
  228.             case C_DO:            /* received command */
  229.                dprintf(stderr,"%s received command from %d (%s)\r\n",
  230.                        name,id,file);
  231.                sprintf(message,"%c%c",S_REPLY,R_RCVD);
  232.                m_sendto(id,message);
  233.                set_icon(&icons[RUNNING]);
  234.  
  235.                /* no file name in command, use snarf buffer */
  236.  
  237.                if (*file == C_SNARF) {
  238.                   m_put();
  239.                   m_gets(file);
  240.                   dprintf(stderr,"%s snarfing (%s)\r\n", name,file);
  241.                   }
  242.  
  243.                /* get host name (if none, assume local host) */
  244.  
  245.                if (path=index(file,HOST)) {
  246.                   *path = '\0';
  247.                   client_host = file;
  248.                   path++;
  249.                   }
  250.                else {
  251.                   path = file;
  252.                   client_host = do_host;
  253.                   }
  254.  
  255.                dprintf(stderr,"Got %s at %s\n",path,client_host);
  256.               
  257.                if (strcmp(client_host,do_host)!= 0)
  258.                   switch (remote) {
  259.                      case NONE:        /* no remote access allowed */ 
  260.                         sprintf(message,"%c%c",S_REPLY,R_HOST);
  261.                         m_sendto(id,message);
  262.                         bailout++;
  263.                         break;
  264.                      case LOCAL:    /* ship file to command */
  265.                         sprintf(temp,"TEMP/%s:%s.%d",
  266.                                client_host,path,getpid());
  267.                         sprintf(message,"rcp %s:%s %s",
  268.                                client_host,path,temp);
  269.                         if (system(message)) {
  270.                            sprintf(message,"%c%c",S_REPLY,R_NET);
  271.                            m_sendto(id,message);
  272.                            bailout++;
  273.                            }
  274.                         break;
  275.                      case REMOTE:    /* run command remotely */
  276.                         sprintf(message,"rsh %s '%s %s'",
  277.                                 client_host,command,path);
  278.                         break;
  279.                      }
  280.                else
  281.                   sprintf(message,"%s %s",command,path);
  282.                
  283.                if (bailout)
  284.                   break;
  285.  
  286.                if (nowindow) {
  287.                   m_ttyreset();
  288.                   dprintf(stderr,"%s Running [%s] no window\n",name,message);
  289.                   code = system(message);
  290.                   m_ttyset();
  291.                   }
  292.                else 
  293.                   code = do_command(message,0,0,font,xpos,ypos,wide,high,wait);
  294.                set_icon(&icons[NORMAL]);
  295.                sprintf(message,"%c%c",S_REPLY,code==0?R_DONE:R_BAD);
  296.                m_sendto(id,message);
  297.                m_clearmode(M_ACTIVATE);
  298.                break;
  299.             case C_WHO:            /* received who-are-you inquiry */
  300.                dprintf(stderr,"%s received query from %d\r\n",name,id);
  301.                sprintf(message,"%c%c %s",S_REPLY,R_WHO,name);
  302.                m_sendto(id,message);
  303.                break;
  304.             case S_HI:            /* Hi, I'm a server */
  305.                dprintf(stderr,"%s Server HI message, ignored\r\n",name);
  306.                alarm(0);
  307.                break;
  308.             case S_BYE:            /* server died */
  309.                dprintf(stderr,"%s Server BYE message, ignored\r\n",name);
  310.                break;
  311.             case S_REPLY:        /* someone thinks I'm a client */
  312.                dprintf(stderr,"%s Oops, I got a server reply message\r\n",name);
  313.                break;
  314.             default:            /* unknown message */
  315.                sprintf(message,"%c%c",S_REPLY,R_UNKWN);
  316.                m_sendto(id,message);
  317.                dprintf(stderr,"%s unknown message\r\n",name);
  318.                break;
  319.             }
  320.          }
  321.       else {
  322.  
  323.          /* process mgr events */
  324.  
  325.          dprintf(stderr,"%s Not a message: %s\r\n",name,line);
  326.          switch(*line) {
  327.             case 'M':                /* moved */
  328.               get_size(&wx,&wy,0,0);
  329.               break;
  330.             case 'S':                /* reshape */
  331.               get_size(&wx,&wy,0,0);
  332.               reset_icon();
  333.               break;
  334.             case 'R':                /* redraw */
  335.               reset_icon();
  336.               break;
  337. #ifdef OOPS
  338.             case 'A':                /* window activated */
  339.               if (icon_count > ACTIVE)
  340.                  set_icon(&icons[ACTIVE]);
  341.               break;
  342.             case 'D':                /* window deactivated */
  343.               if (icon_count > ACTIVE)
  344.                  set_icon(&icons[NORMAL]);
  345.               break;
  346. #endif
  347.             }
  348.          }
  349.       m_flush();
  350.       }
  351.    }
  352.    
  353. /* down load an icon */
  354.  
  355. download_icon(icon,name,where,font)
  356. register struct icon *icon;    /* name of icon to download */
  357. char *name;            /* text string in lieu of icon */
  358. int where;            /* bitmap to download icon to */
  359. int font;            /* char font if no icon */
  360.    {
  361.    int size;
  362.    char line[100];
  363.    int w_in=0,h_in=0;
  364.  
  365.    /* look only on the local machine */
  366.  
  367.    dprintf(stderr,"   looking for %s\r\n",name);
  368.    m_bitfromfile(where,name);
  369.    m_flush();
  370.    m_gets(line);
  371.    sscanf(line,"%d %d",&w_in,&h_in);
  372.    dprintf(stderr,"   Found %s (%d x %d)\r\n",name,w_in,h_in);
  373.    icon->w = w_in;
  374.    icon->h = h_in;
  375.    icon->type = (h_in && w_in) ? where : font;
  376.    icon->name = name;
  377.    return(w_in && h_in);
  378.    } 
  379.  
  380. /* put icon in a window */
  381.  
  382. set_icon(name)
  383. struct icon *name;        /* name of icon */
  384.    {
  385.    m_push(P_EVENT);
  386.    if (have_icon(name)) {
  387.       m_font(1);
  388.       dprintf(stderr,"Setting icon %s\r\n",name->name);
  389.       m_shapewindow(wx,wy,name->w+2*border,name->h+2*border);
  390.       m_bitcopyto(0,0,name->w,name->h,0,0,0,name->type);
  391.       }
  392.    else {
  393.       dprintf(stderr,"Setting text %s\r\n",name->name);
  394.       m_font(name->type);
  395.       m_size(strlen(name->name),1);
  396.       m_clear();
  397.       m_printstr(name->name);
  398.       }
  399.    m_flush();
  400.    m_clearmode(M_ACTIVATE);
  401.    m_pop();
  402.    icon = name;
  403.    }
  404.  
  405. reset_icon()
  406.    {
  407.    if (icon)
  408.       set_icon(icon);
  409.    }
  410.  
  411. /* time out */
  412.  
  413. int
  414. timeout(n)
  415. int n;
  416.    {
  417.    fprintf(stderr,"Can't send messages, sorry\n");
  418.    clean(n);
  419.    }
  420.  
  421. /*    Clean up and exit */
  422.  
  423. clean(n)
  424. int n;
  425.    {
  426.    char message[40];
  427.  
  428.    sprintf(message,"%c",S_BYE);
  429.    m_broadcast(message);
  430.    m_gets(message);
  431.    m_popall();
  432.    m_clear();
  433.    m_ttyreset();
  434.    exit(n);
  435.    }
  436.  
  437. /* run  a command in a sub window */
  438.  
  439. do_command(command,menu,count,font,xpos,ypos,wide,high,wait)
  440. char *command;
  441. struct menu_entry *menu;
  442. int count;
  443. int font,xpos,ypos,wide,high;
  444. int wait;
  445.    {
  446.    int code;        /* return code from command */
  447.    int n;        /* sub-window number */
  448.  
  449.    n=m_makewindow(xpos,ypos,wide,high);
  450.  
  451.    if (n==0) {            /* can't make window */
  452.       m_printstr("\007\fCan't open command window, sorry");
  453.       return(-1);
  454.       }
  455.  
  456.    m_selectwin(n);
  457.    if (count) {
  458.       menu_load(1,count,menu);
  459.       m_selectmenu(1);
  460.       }
  461.    if (font > 0)
  462.       m_font(font);
  463.    m_ttyreset();
  464.    dprintf(stderr,"Running [%s]\n",command);
  465.    code = system(command);
  466.    if (wait) {
  467.       char temp[10];
  468.       m_gets(temp);
  469.       }
  470.    m_ttyset();
  471.    m_selectwin(0);
  472.    m_destroywin(n);
  473.    return(code);
  474.    }
  475.